import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '@/layout/index.vue'
import Login from '@/views/Login/index.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Layout,
    children: [
      {
        path: '',
        component: () => import('@/views/Dashboard/index.vue')
      },
      {
        path: 'category',
        component: () => import('@/views/Category/index.vue')
      },
      {
        path: 'baseConfig',
        component: () => import('@/views/BaseConfig/index.vue')
      },
      {
        path: 'product',
        component: () => import('@/views/Product/index.vue')
      },
      {
        path: 'ordermanager',
        component: () => import('@/views/Order/index.vue')
      },
      {
        path: 'custome',
        component: () => import('@/views/Custome/index.vue')
      },
      {
        path: 'waiter',
        component: () => import('@/views/Waiter/index.vue')
      },
      {
        path: "carousel",
        component: () => import('@/views/Carousel/index.vue')
      },
      {
        path: 'log',
        component: () => import('@/views/SysLog/index.vue')
      },
      {
        path: 'baseConfig',
        component: () => import('@/views/BaseConfig/index.vue')
      },
      {
        path: 'ownerIndex',
        component: () => import('@/views/UserInfo/index.vue')
      },
      {
        path: 'sys/user',
        component: () => import('@/views/RoleAccess/UserManager/index.vue')
      },
      {
        path: 'sys/userDetails',
        component: () => import('@/views/RoleAccess/UserDetails/index.vue')
      },
      {
        path: 'sys/role',
        component: () => import('@/views/RoleAccess/RoleManager/index.vue')
      },
      {
        path: 'sys/privilege',
        component: () => import('@/views/RoleAccess/AuthManager/index.vue')
      },
      {
        path: 'info/list',
        component: () => import('@/views/SiteNews/NewsInfo/index.vue')
      },
      {
        path: 'info/details',
        component: () => import('@/views/SiteNews/DetailsInfo/index.vue')
      },
      {
        path: 'info/publish',
        component: () => import('@/views/SiteNews/PlublishInfo/index.vue')
      },
      {
        path: 'info/category',
        component: () => import('@/views/SiteNews/CategoryInfo/index.vue')
      },
      {
        path: 'account/custome',
        component: () => import('@/views/Account/Custome/index.vue')
      },
      {
        path: "account/employee",
        component: () => import('@/views/Account/Employee/index.vue')
      },
      {
        path: 'check/certification',
        component: () => import('@/views/Check/Certifilcation/index.vue')
      },
      {
        path: 'check/accountApply',
        component: () => import('@/views/Check/AccountApply/index.vue')
      },
      {
        path: 'check/infoComment',
        component: () => import('@/views/Check/InfoComment/index.vue')
      },
      {
        path: 'check/orderComment',
        component: () => import('@/views/Check/OrderComment/index.vue')
      },
    ]
  }
]

// const router = new VueRouter({
//   mode: 'history',
//   base: process.env.BASE_URL,
//   routes
// })
const createRouter = () => new VueRouter({
  mode: 'history',
  scrollBehavior: () => ({ y: 0 }),
  routes
})
const router = createRouter()

// 重写push方法
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

// 路由拦截
router.beforeEach((to, from, next) => {
  // 从 sessionStorage 中读取 token
  const token = sessionStorage.getItem("token");
  const isAuthenticated = !!token;
  // 如果目标路由需要认证且未登录，重定向到登录页
  if (to.matched.some((record) => record.meta.requiresAuth) && !isAuthenticated) {
    next("/login");
  }
  // 如果已登录但尝试访问登录页，重定向到首页
  else if (to.path === "/login" && isAuthenticated) {
    next("/home");
  }
  // 其他情况放行
  else {
    next();
  }
});

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
